var t = require;
var e = module;
var o = exports;
var a,
    i =
        (this && this.__extends) ||
        ((a = function (t, e) {
            return (a =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (t, e) {
                        t.__proto__ = e;
                    }) ||
                function (t, e) {
                    for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
                })(t, e);
        }),
        function (t, e) {
            a(t, e);
            function o() {
                this.constructor = t;
            }
            t.prototype = null === e ? Object.create(e) : ((o.prototype = e.prototype), new o());
        });
Object.defineProperty(o, "__esModule", {value: !0});
var n = t("TearImgTool"),
    r = cc.gfx,
    s = new r.VertexFormat([
        {name: r.ATTR_POSITION, type: r.ATTR_TYPE_FLOAT32, num: 2},
        {name: r.ATTR_UV0, type: r.ATTR_TYPE_FLOAT32, num: 2},
        {name: r.ATTR_COLOR, type: r.ATTR_TYPE_UINT8, num: 4, normalize: !0}
    ]),
    c = (function (t) {
        i(e, t);
        function e() {
            var e = t.call(this) || this;
            e.verticesCount = 4;
            e.indicesCount = 6;
            e.floatsPerVert = 5;
            e.uvOffset = 2;
            e.colorOffset = 4;
            e.indicesArr = [];
            e._renderData = null;
            e._renderData = new cc.RenderData();
            e._renderData.init(e);
            e.initData();
            return e;
        }
        Object.defineProperty(e.prototype, "verticesFloats", {
            get: function () {
                return this.verticesCount * this.floatsPerVert;
            },
            enumerable: !1,
            configurable: !0
        });
        e.prototype.getVfmt = function () {
            return s;
        };
        e.prototype.getBuffer = function () {
            return cc.renderer._handle.getBuffer("mesh", this.getVfmt());
        };
        e.prototype.initData = function () {
            this._renderData.createQuadData(0, this.verticesFloats, this.indicesCount);
        };
        e.prototype.resetData = function (t) {
            var e = t.polygon;
            if (e && !(e.length < 3)) {
                this.verticesCount = e.length;
                this.indicesCount = this.verticesCount + 2 * (this.verticesCount - 3);
                this._renderData.clear();
                this.initData();
            }
        };
        e.prototype.updateColor = function (t, e) {
            var o = this._renderData.uintVDatas[0];
            if (o) {
                e = null != e ? e : t.node.color._val;
                for (var a = this.floatsPerVert, i = this.colorOffset, n = t.polygon, r = 0; r < n.length; r++)
                    o[i + r * a] = e;
            }
        };
        e.prototype.updateUVs = function (t) {
            var e = this.uvOffset,
                o = this.floatsPerVert,
                a = this._renderData.vDatas[0],
                i = [];
            t.texture && (i = n.default.computeUv(t.polygon, t.texture.width, t.texture.height));
            for (var r = t.polygon, s = 0; s < r.length; s++) {
                var c = o * s + e;
                a[c] = i[s].x;
                a[c + 1] = i[s].y;
            }
        };
        e.prototype.updateWorldVertsWebGL = function (t) {
            var e = this._renderData.vDatas[0],
                o = t.node._worldMatrix.m,
                a = o[0],
                i = o[1],
                n = o[4],
                r = o[5],
                s = o[12],
                c = o[13],
                p = 1 === a && 0 === i && 0 === n && 1 === r,
                l = this.floatsPerVert;
            if (p)
                for (var d = t.polygon, u = 0; u < d.length; u++) {
                    e[u * l] = d[u].x + s;
                    e[u * l + 1] = d[u].y + c;
                }
            else
                for (d = t.polygon, u = 0; u < d.length; u++) {
                    e[u * l] = a * d[u].x + n * d[u].y + s;
                    e[u * l + 1] = i * d[u].x + r * d[u].y + c;
                }
        };
        e.prototype.updateWorldVertsNative = function (t) {
            for (var e = this._renderData.vDatas[0], o = this.floatsPerVert, a = t.polygon, i = 0; i < a.length; i++) {
                e[i * o] = a[i].x;
                e[i * o + 1] = a[i].y;
            }
        };
        e.prototype.updateWorldVerts = function (t) {
            this.updateWorldVertsNative(t);
        };
        e.prototype.updateVerts = function (t) {
            this.indicesArr = n.default.splitPolygonByTriangle(t.polygon);
            this.updateWorldVerts(t);
        };
        e.prototype.updateRenderData = function (t) {
            if (t._vertsDirty) {
                this.resetData(t);
                this.updateUVs(t);
                this.updateVerts(t);
                this.updateColor(t, null);
                t._vertsDirty = !1;
            }
        };
        e.prototype.fillBuffers = function (t, e) {
            e.worldMatDirty && this.updateWorldVerts(t);
            var o = this._renderData,
                a = o.vDatas[0],
                i = o.iDatas[0],
                n = this.getBuffer(),
                r = n.request(this.verticesCount, this.indicesCount),
                s = r.byteOffset >> 2,
                c = n._vData;
            a.length + s > c.length ? c.set(a.subarray(0, c.length - s), s) : c.set(a, s);
            for (
                var p = n._iData, l = r.indiceOffset, d = r.vertexOffset, u = this.indicesArr, h = 0;
                h < i.length;
                h++
            )
                p[l++] = d + u[h];
        };
        e.prototype.packToDynamicAtlas = function (t, e) {
            if (!e._original && cc.dynamicAtlasManager && e._texture.packable) {
                var o = cc.dynamicAtlasManager.insertSpriteFrame(e);
                o && e._setDynamicAtlasFrame(o);
            }
            var a = t._materials[0];
            if (a && a.getProperty("texture") !== e._texture) {
                t._vertsDirty = !0;
                t._updateMaterial();
            }
        };
        return e;
    })(cc.Assembler);
o.default = c;
